package com.chixing.server.listener;

import com.alibaba.fastjson.JSON;
import com.chixing.mapper.FlowMapper;
import com.chixing.mapper.MyCouponMapper;
import com.chixing.mapper.MyOrderMapper;
import com.chixing.mapper.StoreCarMapper;
import com.chixing.pojo.*;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class ReceiverOrderPay {
    @Autowired
    private MyOrderMapper myOrderMapper;
    @Autowired
    private StoreCarMapper storeCarMapper;
    @Autowired
    private FlowMapper flowMapper;
    @Autowired
    private MyCouponMapper myCouponMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    //订单支付已完成
    @RabbitHandler
    @RabbitListener(queues = "orderSuccessPayQueue")
    public void process(String context){
        System.out.println("Receiver : " + context);
        MyOrder myOrder =  JSON.parseObject(context,MyOrder.class) ;
            myOrder.setStatus(2);
            //添加订单
        String redisMyOrderKey = "myOrderId_" + myOrder.getMyOrderId();
        myOrderMapper.insert(myOrder);
        redisTemplate.delete(redisMyOrderKey);
        //使用优惠券
        Integer couponId = myOrder.getCouponId();
        Integer custId = myOrder.getCustId();
        if(couponId != null && custId!= null){
            MyCouponExample myCouponExample = new MyCouponExample();
            MyCouponExample.Criteria criteria = myCouponExample.createCriteria();
            criteria.andCustIdEqualTo(custId).andCouponIdEqualTo(couponId);
            MyCoupon myCoupon = myCouponMapper.selectByExample(myCouponExample).get(0);
            myCoupon.setStatus(1);
            myCouponMapper.updateByPrimaryKey(myCoupon);
        }
//        System.out.println("支付成功状态是"+myOrder.getStatus());
    }
//订单支付未完成（已取消）
    @RabbitHandler
    @RabbitListener(queues = "orderFailPayQueue")
    public void process1(String context){
        System.out.println("Receiver : " + context);
        MyOrder myOrder = JSON.parseObject(context,MyOrder.class);
        String redisMyOrderKey = "myOrderId_" + myOrder.getMyOrderId();
        myOrder.setStatus(4);
        myOrderMapper.insert(myOrder);
        redisTemplate.delete(redisMyOrderKey);
//        myOrderMapper.updateByPrimaryKey(myOrder);
//        System.out.println("支付失败的状态是"+myOrder.getStatus());
    }

    //库存减一，销量加一
    @RabbitHandler
    @RabbitListener(queues = "updateStoreCarQueue")
    public void process2(String context){
        System.out.println("Receiver : " + context);
        StoreCar storeCar = JSON.parseObject(context,StoreCar.class);
        storeCarMapper.updateByPrimaryKey(storeCar);
    }

    //添加流水
    @RabbitHandler
    @RabbitListener(queues = "insertFlowQueue")
    public void process3(String context){
        System.out.println("Receiver:" + context);

        Flow flow = JSON.parseObject(context,Flow.class);
        String redisFlowKey = "flowId_" + flow.getFlowNumber();
        flowMapper.insert(flow);
        redisTemplate.delete(redisFlowKey);
    }

}
